package cn.itcast.bos.web.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;

import cn.itcast.bos.domain.base.Area;
import cn.itcast.bos.service.AreaService;
import cn.itcast.bos.utils.PinYin4jUtils;
import cn.itcast.bos.web.action.common.BaseAction;

@ParentPackage("json-default")
@Namespace("/")
@Scope("prototype")
@Controller
public class AreaAction extends BaseAction<Area> {
	private static final long serialVersionUID = 1L;
	@Autowired
	private AreaService areaService;
	

	private File file;
	private String fileFileName;
	private String fileContentType;
	public void setFile(File file) {
		this.file = file;
	}
	public void setFileFileName(String fileFileName) {
		this.fileFileName = fileFileName;
	}
	public void setFileContentType(String fileContentType) {
		this.fileContentType = fileContentType;
	}
	
	@Action(value="area_batchImport",results={@Result(name="success",type="json")})
	public String importArea() throws IOException{
		//定义一个集合接收area对象
		List<Area> list = new ArrayList<>();
		//加载excel文件对象
		HSSFWorkbook hssfWorkbook = new HSSFWorkbook(new FileInputStream(file));
		//读取一个sheet文件
		HSSFSheet sheet = hssfWorkbook.getSheetAt(0);
		//遍历sheet的每一行数据
		for (Row row : sheet) {
			if(row.getRowNum()==0){
				continue;//跳过第一行,标题行
			}
			if(row.getCell(0)== null || StringUtils.isBlank(row.getCell(0).getStringCellValue())){
				continue;//跳过空值的行
			}
			//封装area对象
			Area area = new Area();
			area.setId(row.getCell(0).getStringCellValue());
			area.setProvince(row.getCell(1).getStringCellValue());
			area.setCity(row.getCell(2).getStringCellValue());
			area.setDistrict(row.getCell(3).getStringCellValue());
			area.setPostcode(row.getCell(4).getStringCellValue());
			
			String province = area.getProvince();
			province = province.substring(0, province.length()-1);
			String city = area.getCity();
			city = city.substring(0, city.length()-1);
			String district = area.getDistrict();
			district = district.substring(0, district.length()-1);
			
			//获取shortcode
			String[] shortCodeArr = PinYin4jUtils.getHeadByString(province+city+district);
			StringBuilder sb = new StringBuilder();
			for (String code : shortCodeArr) {
				sb.append(code);
			}
			String shortCode = sb.toString();
			
			String cityCode = PinYin4jUtils.hanziToPinyin(city);
			
			area.setShortcode(shortCode);
			area.setCitycode(cityCode);
			
			//存放在list中
			list.add(area);
			
			
		}
		
		areaService.saveAreas(list);
		return SUCCESS;
	}
	
	/**分页有条件查询area*/
	@Action(value="area_pageQuery",results={@Result(name="success",type="json")})
	public String pageQuery(){
		//条件查询specification
		Specification<Area> specification = new Specification<Area>() {
			@Override
			public Predicate toPredicate(Root<Area> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				Predicate predicate = null;
				List<Predicate> list = new ArrayList<>();
				
				if(StringUtils.isNotBlank(model.getProvince())){
					Predicate p  = cb.like(root.get("province").as(String.class),"%"+ model.getProvince()+"%");
					list.add(p);
				}
				if(StringUtils.isNotBlank(model.getCity())){
					Predicate p = cb.like(root.get("city").as(String.class), "%"+model.getCity()+"%");
					list.add(p);
				}
				if(StringUtils.isNotBlank(model.getDistrict())){
					Predicate p  = cb.like(root.get("district").as(String.class), "%"+model.getDistrict()+"%");
					list.add(p);
				}
				if(list !=null && list.size() != 0){
					Predicate [] preArr = new Predicate[list.size()];
					query.where(list.toArray(preArr));
					return query.getRestriction();
				}
				return null;
			
			}
		};
		
		Pageable pageable = new PageRequest(page-1, rows);
		Page<Area> page  = areaService.pageQuery( specification, pageable);
		
		pushPageToValueStack(page);
		
		return SUCCESS;
	}
	
	
	
	
}





